<?php
class Admin_Form_ValidateLanguage{

	private $_arrError;
	
	private $_arrData;
	
	public function __construct($arrParam = null, $options = null){
		
		$validator = new Zend_Validate();
		$pattern = '#^[a-zA-Z0-9\s\._\']+$#'; //1-n
		
		$validator->addValidator(new Zend_Validate_NotEmpty(),true)
				  ->addValidator(new Zend_Validate_Regex($pattern),true)
				  ->addValidator(new Zend_Validate_StringLength(array('min'=>3,'max'=>50,'encoding'=>'utf-8')),true);
		
		if(!$validator->isValid($arrParam['name'])){
			$message = $validator->getMessages();
			$this->_arrError['name'] = current($message);
			$arrParam['name'] = '';
		}		
		
		$validator = new Zend_Validate();
		if($arrParam['action'] == 'add'){
			$clause = 'area ="' . trim($arrParam['area']) . '"';
			$options = array('table'=>'languages','field'=>'code','exclude'=>$clause);
		}elseif($arrParam['action'] == 'edit'){
			
			$options = array('table'=>'languages',
							 'field'=>'code',
							 'exclude'=>array('field'=>'id',
						 				 	  'value'=>$arrParam['id']));
		}
		
		$pattern = '#^[a-zA-Z0-9\s\._\']+$#'; //1-n
		
		$validator->addValidator(new Zend_Validate_NotEmpty(),true)
				  ->addValidator(new Zend_Validate_Regex($pattern),true)
				  ->addValidator(new Zend_Validate_StringLength(array('min'=>2,'max'=>4,'encoding'=>'utf-8')),true)
				  ->addValidator(new Zend_Validate_Db_NoRecordExists($options),true);
		
		if(!$validator->isValid($arrParam['code'])){
			$message = $validator->getMessages();
			$this->_arrError['code'] = current($message);
			$arrParam['code'] = '';
		}
		
		$validator = new Zend_Validate();
		$pattern = '#^[0-9]*$#'; //1-n
		
		$validator->addValidator(new Zend_Validate_Regex($pattern),true);
		
		if(!$validator->isValid($arrParam['ordering'])){
			$message = $validator->getMessages();
			$this->_arrError['ordering'] = current($message);
			$arrParam['ordering'] = '';
		}
		
		
		$this->_arrData = $arrParam;
	}
	
	public function getData(){		
		return $this->_arrData;
	}
	//return true|false
	public function isVaild(){
		$flag = false;
		if(count($this->_arrError) > 0){
			$flag = true;
		}
		return $flag;
	}
	
	public function getMessageErrors(){
		return $this->_arrError;
	}
	
	
}







